
var istest =0,

 headurl = `http${istest==1?'://test_fuelunion':'s://guanlizhushou'}.wenzengzhang.com/mpapi/`
   ,testurl = `http${istest==1?'://test_fuelunion':'s://guanlizhushou'}.wenzengzhang.com/mpapi/`

// var headurl = `http://test_fuelunion.wenzengzhang.com/mpapi/`
//    ,testurl = `http://test_fuelunion.wenzengzhang.com/mpapi/`
  //,testurl = `https://guanlizhushou.wenzengzhang.com/mpapi/`
  , key = 'wSJyJoPHUANydbf6'
  , secret = 'oPHUANYnLSVAXBkbgQdo'
  , md5 = require('../utils/md5.js')
  , imgurl = `${headurl}imgs/`
  , audiourl = `${headurl}audio/`; 
class api {

  getheadurl() {
    return headurl;
  }

  getstaticimg() {
    return 'https://test.fan921.com/bak/src/static/';
  }
  getimgurl() {
    return imgurl;
  }
  getaudiourl() {
    return audiourl;
  }
  /**
   * 登录
   */
  wxlogin(code, success, fail) {
    postdata('user/onlogin/', {
      code
    }, res => success(res), res => fail(res))
  }

  getuserdetail(succ) {
    postdata('user/detail/', {}, res => succ(res))
  }

  获取超级会员信息(succ) {
   // console.log('获取超级会员信息');
    postdatat('user/superinfo/', {}, res => succ(res)) 
  }

  获取协议(succ) {
    postdatat('system/superrules/', {}, res => succ(res))
  }

  用户绑定超会车牌(carPlate,succ) {
    postdatat('wash/bindcar/', {carPlate}, res => succ(res))
  }

  得到超会车牌信息(succ) {
    postdatat('wash/getcar/', {}, res => succ(res))
  }




  超会月礼品领取(succ) {
    postdatat('coupon/supergift/', {}, res => succ(res))
  }
  超会免费洗车订单(orderId,succ) {
    postdatat('order/superwashpay/', {orderId}, res => succ(res))
  }

  用户提交洗车订单(data,succ) {
    postdatat('wash/place/', data, res => succ(res))
   }

  超级会员下单操作(chargeId,succ) {
    postdatat('charge/superadd/', {chargeId}, res => succ(res))
  }
  超会订单微信发起订单支付(orderId,succ) {
    postdatat('order/superchargewxstartpay/', {orderId}, res1 => {
      console.log(res1.data.code, 'res1');
      if (res1.data.code == 0) {
        // var data = JSON.parse(res.data.entity.data);
        var data = res1.data.entity.data;
        // var orderid = data.orderid;
        console.log(data.timeStamp, 'datadatadata');

        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          signType: data.signType,
          success: res => {
            console.log(res, 'succ')
            if (res.errMsg.indexOf('ok') > -1) {
              succ && succ({
                succ: 1,
                // orderid
              })
            }
          },
          fail: res => {
            //支付取消
            succ && succ({
              succ: -1,
              msg: '支付取消'
            })
          }
        })
      } else {
        succ && succ({
          succ: -1,
          msg: res1.data.message
        })
      }




    })







  }

  getrechargelist(succ) {
    postdata('charge/items/', {}, res => succ(res))
  }
  //加油站列表
  systemstation(data, succ) {
    postdata('system/station/', data, res => succ(res))
  }
  //充值下单操作
  chargeadd(data, succ) {
    postdata('charge/add/', data, res => succ(res))
  }
  //首次设置密码
  usersetpwd(pwd, succ) {
    postdata('user/setpwd/', { pwd }, res => succ(res))
  }
  // 用户充值订单列表
  orderrecord(page, succ) {
    postdata('charge/record/', { page }, res => succ(res))
  }
  // 得到指定油站的加油设备列表
  oildevice(stationId, succ) {
    postdata('oil/device/', { stationId }, res => succ(res))
  }
  //  检测加油订单是否得到优惠券
  oilgrantcoupon(orderId, succ) {
    postdata('oil/grantcoupon/', { orderId }, res => succ(res))
  }
  //  用户提交加油金额生成订单
  oilplace(data, succ) {
    postdatat('oil/place/', data, res => succ(res))
  }
  //可开发票的信息
  invoiceinfo(data, succ) {
    postdata('invoice/info/', data, res => succ(res))
  }
  //开发票申请表单
  invoiceapply(data, succ) {
    postdata('invoice/apply/', data, res => succ(res))
  }
  //用户开发票的记录列表
  invoicerecord(data, succ) {
    postdata('invoice/record/', data, res => succ(res))
  }
  // 订单余额支付
  ordercheckaccountpay(data, succ) {
    postdata('order/checkaccountpay/', data, res => succ(res))
  }
  // 检测加油订单是否得到优惠券
  oilgrantcoupon(orderId, succ) {
    postdata('oil/grantcoupon/', { orderId }, res => succ(res))
  }
  // 用户对加油的评价
  reviewfeel(data, succ) {
    postdata('review/feel/', data, res => succ(res))
  }
  // 忘记支付密码帮助
  systemforgethelp(succ) {
    postdata('system/forgethelp/', {}, res => succ(res))
  }
  // 用户优惠券列表
  couponlists(data, succ) {
    postdata('coupon/lists/', data, res => succ(res))
  }
  // 检测加油订单是否得到优惠券
  coupondetail(cid, succ) {
    postdata('coupon/detail/', { cid }, res => succ(res))
  }
  //  会员权益说明
  systembenefits(succ) {
    postdata('system/benefits/', {}, res => succ(res))
  }
  // 会员规则说明
  systemrules(succ) {
    postdata('system/rules/', {}, res => succ(res))
  }
  // 会员积分
  userscore(succ) {
    postdata('user/score/', {}, res => succ(res))
  }
  // 积分兑换列表
  scoreitems(succ) {
    postdata('score/items/', {}, res => succ(res))
  }
  // 积分记录列表
  scorerecord(data, succ) {
    postdata('score/record/', data, res => succ(res))
  }
  // 积分兑换申请
  scoreapply(couponId, succ) {
    postdata('score/apply/', { couponId }, res => succ(res))
  }
  // 修改密码
  useralterpwd(data, succ) {
    postdata('user/alterpwd/', data, res => succ(res))
  }
  // 添加用户投诉
  reviewcomplaint(data, succ) {
    postdata('review/complaint/', data, res => succ(res))
  }
  // 用户加油订单列表
  oilrecord(page, succ) {
    postdata('oil/record/', { page }, res => succ(res))
  }
  // 用户消费绘图
  usergather(month, succ) {
    postdata('user/gather/', { month }, res => succ(res))
  }
  // 用户微信信息更新
  updatewxinfo(data, succ) {
    postdata('user/updatewxinfo/', data, res => succ(res))
  }
  // 会员规则说明 怎样获得更多优惠
  systemrules(succ) {
    postdata('system/rules/', {}, res => succ(res))
  }
  // 开发票二维码处理状态
  invoicedealstatus(qrStr, succ) {
    postdata('invoice/dealstatus/', { qrStr }, res => succ(res))
  }
  // 获取加油订单详情
  oildetail(orderId, succ) {
    postdatat('oil/detail/', { orderId }, res => succ(res))
  }
  //  授权获取用户手机号
  wxphone(data, succ) {
    postdata('user/wxphone/', data, res => succ(res))
  }
  //  获取当前企业的信息
  systemcorpinfo(succ) {
    postdata('system/corpinfo/', {}, res => succ(res))
  }
  //  迁移旧用户到新系统
  transferold(oldRel, succ) {
    postdata('user/transferold/', { oldRel }, res => succ(res))
  }
  //  取消加油订单
  oilcancel(orderId, succ) {
    postdata('oil/cancel/', { orderId }, res => succ(res))
  }
  //  得到团购的列表
  getbulkList(data, succ) {
    postdata('bulk/items/', data, res => succ(res))
  }
  // 得到团购的详情
  getbulkDetail(bid, succ) {
    postdata('bulk/detail/', { bid }, res => succ(res))
  }
  // 得到非油品订单的列表
  goodsrecord(page, succ) {
    postdata('goods/record/', { page }, res => succ(res))
  }
  // 新增用户收货地址
  addaddress(data, succ) {
    postdata('address/add/', data, res => succ(res))
  }
  // 修改用户收货地址
  editaddress(data, succ) {
    postdata('address/edit/', data, res => succ(res))
  }
  // 得到用户收货地址的列表
  getaddressList(succ) {
    postdata('address/record/', {}, res => succ(res))
  }
  // 得到用户收货地址的详情
  getaddressDetail(aid, succ) {
    postdata('address/detail/', { aid }, res => succ(res))
  }
  // 预算购买消费金额
  checkbudget(data, succ) {
    postdata('bulk/checkbudget/', data, res => succ(res))
  }
  // 得到团购订单的列表
  getorderrecord(page, succ) {
    postdata('bulk/orderrecord/', { page }, res => succ(res))
  }
  // 得到团购订单的详情
  getorderrecordDetail(orderId, succ) {
    postdata('bulk/orderdetail/', { orderId }, res => succ(res))
  }
  // 团购订单申请售后
  aftersaleorder(data, succ) {
    postdata('bulk/aftersaleorder/', data, res => succ(res))
  }
  // 团购订单确认收货
  confirmorder(orderId, succ) {
    postdata('bulk/confirmorder/', { orderId }, res => succ(res))
  }
  // 购买团购下单
  placeorder(data, succ) {
    postdata('bulk/place/', data, res => succ(res))
  }
  // 团购--账户余额支付处理
  bulkaccountpay(data, succ) {
    postdata('order/bulkaccountpay/', data, res => succ(res))
  }
  getfocus(posId, succ) {
    postdata('bulk/focus/', { posId }, res => succ(res))
  }
  // 检测非油订单是否得到优惠券
  fypgrantcoupon(orderId, succ) {
    postdata('goods/grantcoupon/', { orderId }, res => succ(res))
  }



  //用户提交非油品订单
  goodsplace(data, succ) {
    postdata('goods/place/', data, res => succ(res))
  }
  //得到非油品订单的详情
  goodsdetail(orderId, succ) {
    postdata('goods/detail/', { orderId }, res => succ(res))
  }
  //得到油站的洗车类型
  washcate(stationId, succ) {
    postdata('wash/cate/', { stationId }, res => succ(res))
  }
  //得到用户有效的洗车凭证
  washmycard(stationId, succ) {
    postdata('wash/mycard/', { stationId }, res => succ(res))
  }
  //用户提交洗车订单
  washplace(data, succ) {
    postdata('wash/place/', data, res => succ(res))
  }
  //非油品--账户余额支付处理
  goodsaccountpay(data, succ) {
    postdata('order/goodsaccountpay/', data, res => succ(res))
  }
  //洗车--账户余额支付处理
  washaccountpay(data, succ) {
    postdata('order/washaccountpay/', data, res => succ(res))
  }
  //得到团购弹窗推荐信息
  bulkpopon(posId, succ) {
    postdata('bulk/popon/', { posId }, res => succ(res))
  }
  //得到团购弹窗推荐信息
  washdetail(orderId, succ) {
    postdata('wash/detail/', { orderId }, res => succ(res))
  }

  vipwashdetail(orderId, succ) {
    postdatat('wash/detail/', { orderId }, res => succ(res))
  }


  // 0322
  //得到团购弹窗推荐信息
  washrecord(page, succ) {
    postdatat('wash/record/', { page }, res => succ(res))
  }
  //添加对洗车的评价
  reviewwashfeel(data, succ) {
    postdata('review/washfeel/', data, res => succ(res))
  }

  washspecailpay(data, succ) {
    postdata('order/washspecialpay/', data, res => succ(res))
  }
  // 检测充值订单是否得到优惠券
  chargegrantcoupon(orderId, succ) {
    postdata('charge/grantcoupon/', { orderId }, res => succ(res))
  }
  // 检测团购订单是否得到优惠券
  bulkgrantcoupon(orderId, succ) {
    postdata('bulk/grantcoupon/', { orderId }, res => succ(res))
  }
  // 检测充值订单是否得到分享机会
  chargegrantshare(orderId, succ) {
    postdata('charge/grantshare/', { orderId }, res => succ(res))
  }
  // 用户抢分享券
  userGrabcoupon(sk, succ) {
    postdata('share/grab/', { sk }, res => succ(res))
  }
  // 用户发起一次分享
  usershare(succ) {
    postdata('share/give/', {}, res => succ(res))
  }
  // 根据分享key得到分享的详情
  getshareDetail(sk, succ) {
    postdata('share/detail/', { sk }, res => succ(res))
  }
  // 得到用户分享的列表
  getshareRecord(page, succ) {
    postdata('share/record/', { page }, res => succ(res))
  }
  // 优惠券核销状态
  couponcheckstate(cid, succ) {
    postdata('coupon/check/', { cid }, res => succ(res))
  }
  // 检测订单核销状态
  ordercheckdeal(orderId, succ) {
    postdata('bulk/checkdeal/', { orderId }, res => succ(res))
  }
  // 团购类别列表
  getcatelist(succ) {
    postdata('bulk/catelist/', {}, res => succ(res))
  }

  test_cleanvip(id,succ) {
    getdatat(id, res => succ(res))
  }

  // 团购热门推荐
  gethotset(succ) {
    postdata('bulk/hotset/', {}, res => succ(res))
  }
  // 得到团购的列表-最新排列
  getallgoods(page, succ) {
    postdata('bulk/lists/', { page }, res => succ(res))
  }
  // 团购类别和搜索页
  searchgoods(data, succ) {
    postdata('bulk/search/', data, res => succ(res))
  }

  // 团购-微信发起订单支付
  bulkwxstartpay(data, succ, fail) {
    // data['openid'] = wx.getStorageSync('openid')
    postdata('order/bulkwxstartpay/', data, res1 => {
      console.log(res1.data.code, 'res1');
      if (res1.data.code == 0) {
        // var data = JSON.parse(res.data.entity.data);
        var data = res1.data.entity.data;
        // var orderid = data.orderid;
        console.log(data.timeStamp, 'datadatadata');

        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          signType: data.signType,
          success: res => {
            console.log(res, 'succ')
            if (res.errMsg.indexOf('ok') > -1) {
              succ && succ({
                succ: 1,
                // orderid
              })
            }
          },
          fail: res => {
            //支付取消
            succ && succ({
              succ: -1,
              msg: '支付取消'
            })
          }
        })
      } else {
        succ && succ({
          succ: -1,
          msg: res1.data.message
        })
      }
    })
  }





  uploadimg(data, params) {
    // console.log(data,params,"q11fesfds");
    // return;
    let ts = parseInt(new Date().getTime() / 1000)
    var that = this,
      i = data.i ? data.i : 0, //当前上传的哪张图片
      success = data.success ? data.success : 0, //上传成功的个数
      fail = data.fail ? data.fail : 0, //上传失败的个数
      pics = data.pics ? data.pics : [];
    // console.log(data.path[i]);
    wx.uploadFile({
      header: {
        'content-type': "application/x-www-form-urlencoded",
        // token: getApp().logininfo.Token,
        // userid: getApp().logininfo.userid
        'X-User-Key': key,
      'X-User-Ts': ts,
      'X-User-Sign': md5.hexMD5(ts + key + secret),
      'X-User-Sign': md5.hexMD5(ts + key + secret),
      'X-User-UserId': getApp().logininfo ? getApp().logininfo.uid : 0,
      'X-User-UserCode': getApp().logininfo ? getApp().logininfo.code : ''
      },
      url: data.url,
      filePath: data.path[i],
      name: 'avatarFile', //这里根据自己的实际情况改
      formData: null, //这里是上传图片时一起上传的数据
      success: (resp) => {
        success++; //图片上传成功，图片上传成功的变量+1
        let result = JSON.parse(resp.data);
        if(result.data.code==0){
          console.log(result,'resultresult');
          params.success();
        }
      //  pics.push(result.entity.data.picUrl);
        //这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1 
      },
      fail: (res) => {
        fail++; //图片上传失败，图片上传失败的变量+1
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: (e) => {
        console.log(i);
        console.log(e,'complete');

        i++; //这个图片执行完上传后，开始上传下一张            
        if (i == data.path.length) { //当图片传完时，停止调用          
          console.log('执行完毕');
          console.log('成功：' + success + " 失败：" + fail);

          params.success(pics);

        } else { //若图片还没有传完，则继续调用函数                
          console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          data.pics = pics;
          that.uploadimg(data, params);
        }
      }
    });
  }


  chargewxstartpay(data, succ, fail) {
    // data['openid'] = wx.getStorageSync('openid')
    postdata('order/chargewxstartpay/', data, res1 => {
      console.log(res1.data.code, 'res1');

      if (res1.data.code == 0) {
        // var data = JSON.parse(res.data.entity.data);
        var data = res1.data.entity.data;
        // var orderid = data.orderid;
        console.log(data.timeStamp, 'datadatadata');

        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          signType: data.signType,
          success: res => {
            console.log(res, 'succ')
            if (res.errMsg.indexOf('ok') > -1) {
              succ && succ({
                succ: 1,
                // orderid
              })
            }
          },
          fail: res => {
            //支付取消
            succ && succ({
              succ: -1,
              msg: '支付取消'
            })
          }
        })
      } else {
        succ && succ({
          succ: -1,
          msg: res1.data.message
        })
      }
    })
  }


  prepay(data, succ, fail) {
    // data['openid'] = wx.getStorageSync('openid')
    postdatat('order/wxstartpay/', data, res1 => {
      console.log(res1.data.code, 'res1');

      if (res1.data.code == 0) {
        // var data = JSON.parse(res.data.entity.data);
        var data = res1.data.entity.data;
        // var orderid = data.orderid;
        console.log(data.timeStamp, 'datadatadata');

        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          signType: data.signType,
          success: res => {
            console.log(res, 'succ')
            if (res.errMsg.indexOf('ok') > -1) {
              succ && succ({
                succ: 1,
                // orderid
              })
            }
          },
          fail: res => {
            //支付取消
            succ && succ({
              succ: -1,
              msg: '支付取消'
            })
          }
        })
      } else {
        succ && succ({
          succ: -1,
          msg: res1.data.message
        })
      }
    })
  }

  //洗车 支付
  washprepay(data, succ, fail) {
    // data['openid'] = wx.getStorageSync('openid')
    postdata('order/washwxstartpay/', data, res1 => {
      console.log(res1.data.code, 'res1');

      if (res1.data.code == 0) {
        // var data = JSON.parse(res.data.entity.data);
        var data = res1.data.entity.data;
        // var orderid = data.orderid;
        console.log(data.timeStamp, 'datadatadata');
        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          signType: data.signType,
          success: res => {
            console.log(res, 'succ')
            if (res.errMsg.indexOf('ok') > -1) {
              succ && succ({
                succ: 1,
                // orderid
              })
            }
          },
          fail: res => {
            //支付取消
            succ && succ({
              succ: -1,
              msg: '支付取消'
            })
          }
        })
      } else {
        succ && succ({
          succ: -1,
          msg: res1.data.message
        })
      }
    })
  }

  //非油品 支付
  nooilprepay(data, succ, fail) {
    // data['openid'] = wx.getStorageSync('openid')
    postdata('order/goodswxstartpay/', data, res1 => {
      console.log(res1.data.code, 'res1');

      if (res1.data.code == 0) {
        // var data = JSON.parse(res.data.entity.data);
        var data = res1.data.entity.data;
        // var orderid = data.orderid;
        console.log(data.timeStamp, 'datadatadata');

        wx.requestPayment({
          timeStamp: data.timeStamp,
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          signType: data.signType,
          success: res => {
            console.log(res, 'succ')
            if (res.errMsg.indexOf('ok') > -1) {
              succ && succ({
                succ: 1,
                // orderid
              })
            }
          },
          fail: res => {
            //支付取消
            succ && succ({
              succ: -1,
              msg: '支付取消'
            })
          }
        })
      } else {
        succ && succ({
          succ: -1,
          msg: res1.data.message
        })
      }
    })
  }

}

function postdata(urlparms, data, success, fail) {
  let ts = parseInt(new Date().getTime() / 1000)
  wx.request({
    url: headurl + urlparms,
    method: "POST",
    data: data,
    header: {
      // 'content-type': "application/x-www-form-urlencoded",
      'X-User-Key': key,
      'X-User-Ts': ts,
      'X-User-Sign': md5.hexMD5(ts + key + secret),
      'X-User-Sign': md5.hexMD5(ts + key + secret),
      'X-User-UserId': getApp().logininfo ? getApp().logininfo.uid : 0,
      'X-User-UserCode': getApp().logininfo ? getApp().logininfo.code : ''
    },
    success: res => success(res),
    fail: res => fail && fail(res)
  })
}

function postdatat(urlparms, data, success, fail) {
  let ts = parseInt(new Date().getTime() / 1000)
  wx.request({
    url: testurl + urlparms,
    method: "POST",
    data: data,
    header: {
      // 'content-type': "application/x-www-form-urlencoded",
      'X-User-Key': key,
      'X-User-Ts': ts,
      'X-User-Sign': md5.hexMD5(ts + key + secret),
      'X-User-Sign': md5.hexMD5(ts + key + secret),
      'X-User-UserId': getApp().logininfo ? getApp().logininfo.uid : 0,
      'X-User-UserCode': getApp().logininfo ? getApp().logininfo.code : ''
    },
    success: res => success(res),
    fail: res => fail && fail(res),
    complete:res=>{
      //console.log(res,'complete--postdatat');
    }
  })
}


function getdatat(uid,success, fail) {

  wx.request({
    url: 'http://test_fuelunion.wenzengzhang.com/index/hello?uid='+uid,
    header: {
      'content-type': "application/x-www-form-urlencoded",
    },
    success: res => success(res),
    fail: res => fail && fail(res)
  })
}

function getdata(urlparms, data, success, fail) {

  wx.request({
    url: headurl + urlparms,
    data: data,
    header: {
      'content-type': "application/x-www-form-urlencoded",
      'userId': getApp().userinfo.userId,
    },
    success: res => success(res),
    fail: res => fail && fail(res)
  })
}

function fstpostdata(urlparms, data, success, fail) {
  wx.request({
    url: headurl + urlparms,
    method: "POST",
    data: data,
    header: {
      'content-type': "application/x-www-form-urlencoded",
    },
    success: res => success(res),
    fail: res => fail && fail(res),
    complete: res => console.log('complete', res)
  })
}



module.exports = api